import remove from 'lodash/remove'

let relList = {
  '001': ['ADMIN'],
  '002': ['MANAGER', 'USER'],
  '003': ['USER'],
  '004': ['USER']
}

export default {
  'get|/sys/user/relRole': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const userId = url.split('/')[url.split('/').length - 1]
    const rel = relList[userId]
    console.log(userId)
    if (rel) {
      ret.data = rel
    } else {
      ret.code = 500
      ret.msg = '找不到用户Id为[' + userId + ']的角色信息!'
    }
    return ret
  },
  'put|/sys/user/auth': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const userId = url.split('/')[url.split('/').length - 1]
    const params = JSON.parse(option.body)
    const removeList = params.remove
    const addList = params.add
    remove(relList[userId], function (n) {
      return removeList.indexOf(n) > -1
    })
    relList[userId] = relList[userId].concat(addList)
    ret.msg = '设置角色成功!请关闭mockjs开启后台体验完整功能！'
    return ret
  }
}
